<template>
  <div class="page"
       :style="{width:width+'px',height:height+'px'}">
    <div class="page__bd">
      <div class="weui-tab">
        <div class="weui-navbar">
          <block v-for="(item,index) in tabs"
                 :key="index">
            <div :id="index"
                 :class="{'weui-bar__item_on':activeIndex == index}"
                 class="weui-navbar__item"
                 @click="tabClick">
              <div class="weui-navbar__title">{{item}}</div>
            </div>
          </block>
          <div class="weui-navbar__slider"
               :class="navbarSliderClass"></div>
        </div>
        <div class="weui-tab__panel">
          <div class="noData"
               v-if="noData">
            <img :src=noDataPng
                 alt="">
          </div>
          <div v-if="questionList.length"
               class="showData">
            <div class="weui-tab__content"
                 :hidden="activeIndex != 0">
              <div class="blank"></div>
              <div class="question"
                   v-for="(item,index) in questionList"
                   :key=index>
                <div class="questionNum">
                  第{{index+1}}题
                  <p class="p2"
                     @click.stop="deleteCollect(index,item.id)">
                    <span>删除</span>
                  </p>
                </div>
                <div class="questionContent">
                  <div class="questionTitle">
                    {{questionList.length&&questionList[index].question}}
                  </div>
                  <div class="questionAnswer">
                    <div v-for="(value,key) in questionList.length&&answer_list"
                         :key=key>
                      <p>
                        <span>{{options[key]}}</span>
                        {{value}}
                      </p>
                    </div>
                  </div>
                </div>
                <div class="analyseAnswer">
                  <p>正确答案：{{questionList[index].answer}}</p>
                  <p>所在章节：{{questionList[index].location}} </p>
                  <p>知识点： {{questionList[index].point}} </p>
                  <p>答案解析：</p>
                  <p>{{questionList[index].analysis}}</p>
                </div>
              </div>
            </div>
          </div>

          <div class="weui-tab__content"
               :hidden="activeIndex != 1">
            <div class="blank"></div>
            <div class="noData">
              <img :src=noDataPng
                   alt="">
            </div>
          </div>
          <div class="weui-tab__content"
               :hidden="activeIndex != 2">
            <div class="blank"></div>
            <div class="noData">
              <img :src=noDataPng
                   alt="">
            </div>
          </div>
          <div class="weui-tab__content"
               :hidden="activeIndex != 3">
            <div class="blank"></div>
            <div class="noData">
              <img :src=noDataPng
                   alt="">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      tabs: ['单选题', '多选题', '判断题', '填空题'],
      options: ['A', 'B', 'C', 'D'],
      activeIndex: 0,
      questionList: [],
      index: 0,
      timer: '',
      userid: '',
      noDataPng: './../../static/images/noData.png',
      noData: false
    }
  },
  computed: {
    ...mapState({
      userInfo: state => state.userInfo
    }),
    navbarSliderClass () {
      if (Number(this.activeIndex) === 0) {
        return 'weui-navbar__slider_0'
      }
      if (Number(this.activeIndex) === 1) {
        return 'weui-navbar__slider_1'
      }
      if (Number(this.activeIndex) === 2) {
        return 'weui-navbar__slider_2'
      }
      if (Number(this.activeIndex) === 3) {
        return 'weui-navbar__slider_3'
      }
    },
    answer_list () {
      let that = this
      return that.questionList.length && Object.keys(that.questionList[that.index].answer_list).map(key => that.questionList[that.index].answer_list[key]).sort((a, b) => {
        return a.localeCompare(b)
      })
    },
    observeData () {
      console.log(this.answer_list)
    }
  },
  methods: {
    tabClick (e) {
      if (Number(e)) {
        this.activeIndex = 0
      } else {
        this.activeIndex = e.currentTarget.id
      }
      console.log(e.currentTarget.id)
      wx.showLoading({
        title: '加载中',
        success () {
          wx.hideLoading()
        }
      })
    },
    // 删除习题收藏
    deleteCollect (index, id) {
      let that = this
      wx.showToast({
        title: '删除中',
        icon: 'loading',
        duration: 1000,
        success () {
          that.timer = setTimeout(() => {
            that.questionList.splice(index, 1)
            that.$http.post('https://dati.co-links.com/operat/del_collection/', {
              userid: that.userid,
              questionid: id
            }).then((res) => {
              console.log(res)
            })
          }, 1000)
        }
      })
    },
    init () {
      let that = this
      this.userid = this.userInfo.user_id || wx.getStorageSync('user_id')
      this.questionList = []
      this.noData = false
      console.log('readHistory')
      wx.showLoading({
        title: '加载中'
      })
      this.$http.get('https://dati.co-links.com/operat/red_collection/', {
        userid: that.userid
      }).then(function (res) {
        console.log(res)
        if (res.data.code === '0000') {
          wx.hideLoading()
          if (res.data.data.length === 0) {
            that.noData = true
          }
          that.questionList = res.data.data
        }
        console.log(res)
      })
    }

  },
  mounted () {
    this.init()
  }
}
</script>
<style lang="stylus" scoped>
.page {
  background: #f7f7f7;
  height: 100%;
  width: 100%;
}

.page__bd {
  background: white;
  height: 100%;
}

.weui-navbar__slider_0 {
  left: 0rpx;
  transform: translateX(50%);
}

.weui-navbar__slider_1 {
  left: 25%;
  transform: translateX(50%);
}

.weui-navbar__slider_2 {
  left: 50%;
  transform: translateX(50%);
}

.weui-navbar__slider_3 {
  left: 75%;
  transform: translateX(50%);
}

.weui-navbar__slider {
  width: 100rpx;
}

.weui-navbar {
  border-bottom: 0;
  background: white;
}

.blank {
  height: 20rpx;
  background: #f7f7f7;
}

.question {
  padding: 0 32rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  background: white;

  .questionNum {
    font-size: 30rpx;
    color: #333;
    height: 98rpx;
    line-height: 98rpx;
    border-bottom: 2rpx solid #e6e6e6;

    .p2 {
      float: right;
      width: 112rpx;

      span {
        display: inline-block;
        width: 112rpx;
        height: 46rpx;
        background: #febd32;
        color: white;
        font-size: 26rpx;
        text-align: center;
        line-height: 46rpx;
        border-radius: 6rpx;
        -webkit-border-radius: 6rpx;
      }
    }
  }

  .questionTitle {
    font-size: 32rpx;
    color: #333333;
    padding: 20rpx 0;
  }
}

.questionContent {
  padding-bottom: 30rpx;
}

.questionAnswer {
  p {
    font-size: 32rpx;
    color: #666666;
    padding: 18rpx 0;
    padding-left: 30rpx;

    span {
      display: inline-block;
      width: 48rpx;
      height: 48rpx;
      border-radius: 50%;
      margin-right: 30rpx;
      border: 1px solid #a5a5a5;
      text-align: center;
      line-height: 48rpx;
    }
  }
}

.analyseAnswer {
  padding: 20rpx 22rpx;
  background: #ebebeb;
  font-size: 32rpx;
  color: #333333;
  border-radius: 12rpx;
  -webkit-border-radius: 12rpx;
  margin-bottom: 20rpx;

  p {
    line-height: 60rpx;
  }

  p:last-child {
    font-size: 28rpx;
  }
}

.weui-tab__panel {
  background: #f7f7f7;
}
</style>
